package org.jeecg.modules.xiaonv.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.jeecg.modules.stCaip.entity.HqStCaip;
import org.jeecg.modules.xiaonv.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.*;

/**
 * @author Administrator
 * @date 2020/10/26-9:54
 * @project_name jeecg-boot-parent
 */
@Service
public class XiaoNiuService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${nanjxn.serverUrl}")
    String serverUrl = "";

    String jiam(Map<String, Object> map) {
        String json = JSON.toJSONString(map);
        return restTemplate.postForObject(serverUrl + "/LdRsjq", json, String.class);
    }

    String jiem(String json) {
        return restTemplate.postForObject(serverUrl + "/LdRsYq", json, String.class);
    }

    // 订单号查订单详情
    public List<XiaoNvOrderDetail> cpdatamx(String orderNo) {
        List<XiaoNvOrderDetail> list = null;

        Map<String, Object> map = new HashMap() {{
            put("dbh", orderNo);
            put("karq", "2020-01-01");
            put("jsrq", "2030-12-01");
        }};

        String encrypt = jiam(map);
        try {
            String rtn = restTemplate.postForObject(serverUrl + "/cpdatamx", encrypt, String.class);
            String decrypt = jiem(rtn);
            XiaoNvRtn xiaoNvRtn = JSON.parseObject(decrypt, new TypeReference<XiaoNvRtn>() {});

            list = JSON.parseObject(JSONObject.toJSONString(xiaoNvRtn.getDatas()), new TypeReference<List<XiaoNvOrderDetail>>() {});
            for (XiaoNvOrderDetail xiaoNvOrderDetail: list
                 ) {
                xiaoNvOrderDetail.setCaiptp("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605028825784&di=de68af5e1a84e6896b6f2c2801fc2fdd&imgtype=0&src=http%3A%2F%2Fcp2.douguo.net%2Fupload%2Fcaiku%2Fb%2Fd%2Fd%2F600x400_bdf6889987b629b6c85b97a3158610dd.jpg");

                String caipbh = xiaoNvOrderDetail.getXfMeNo();
                XiaoNCaiPYycf xiaoNCaiPYycf = cpyy(caipbh);
                xiaoNvOrderDetail.setRel(xiaoNCaiPYycf.getReliang());
                xiaoNvOrderDetail.setDanbz(xiaoNCaiPYycf.getDanbaizhi());
                xiaoNvOrderDetail.setZhif(xiaoNCaiPYycf.getZhifang());
                xiaoNvOrderDetail.setTanshhw(xiaoNCaiPYycf.getTanshui());
                xiaoNvOrderDetail.setShansqw(xiaoNCaiPYycf.getQianwei());
                xiaoNvOrderDetail.setDangc(xiaoNCaiPYycf.getDg());
                xiaoNvOrderDetail.setNa(xiaoNCaiPYycf.getNa());
                xiaoNvOrderDetail.setGai(xiaoNCaiPYycf.getGa());
            }

        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return list;
    }

    // 员工消费记录
    public List<XiaoNXfjl> wxLdSsCzXf(String emworkno, String start, String end) {
        List<XiaoNXfjl> list = new ArrayList<>();

        Map<String, Object> map = new HashMap() {{
            put("emworkno", emworkno);
            put("start_dt", start);
            put("end_dt", end);
        }};

        String encrypt = jiam(map);
        try {
            String rtn = restTemplate.postForObject(serverUrl + "/wxLdSsCzXf", encrypt, String.class);
            String decrypt = jiem(rtn);
            XiaoNvRtn xiaoNvRtn = JSON.parseObject(decrypt, new TypeReference<XiaoNvRtn>() {});
            for (Object obj: xiaoNvRtn.getDatas()
                 ) {
                XiaoNXfjl xiaoNXfjl = JSON.parseObject(JSONObject.toJSONString(obj), new TypeReference<XiaoNXfjl>() {});
                list.add(xiaoNXfjl);
            }

        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return list;
    }

    // 菜品图片
    XiaoNCaiP cpdataimg(String cpbh) {
        XiaoNCaiP xiaoNCaiP = null;

        Map<String, Object> map = new HashMap() {{
            put("cpbh", cpbh);
        }};

        String encrypt = jiam(map);
        try {
            String rtn = restTemplate.postForObject(serverUrl + "/cpdataimg", encrypt, String.class);
            String decrypt = jiem(rtn);
            System.out.println(decrypt);
            XiaoNvRtn xiaoNvRtn = JSON.parseObject(decrypt, new TypeReference<XiaoNvRtn>() {});
            byte[] fromBase64 = Base64.getDecoder().decode(xiaoNvRtn.getMpImg());
            OutputStream out = null;
            try {
                out = new BufferedOutputStream(new FileOutputStream("d\\test.jpg"));
                out.write(fromBase64);

            } finally {
                if (out != null) {
                    out.close();
                }
            }

        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return xiaoNCaiP;
    }

    // 获取所有菜品, 不重复带图片带营养成分
    public List<XiaoNvOrderDetail> cpdata() {
        List<XiaoNvOrderDetail> list = null;

        Map<String, Object> map = new HashMap() {{
            put("cpbh", "0");
        }};

        String encrypt = jiam(map);
        try {
            String rtn = restTemplate.postForObject(serverUrl + "/cpdata", encrypt, String.class);
            String decrypt = jiem(rtn);
            System.out.println(decrypt);
            XiaoNvRtn xiaoNvRtn = JSON.parseObject(decrypt, new TypeReference<XiaoNvRtn>() {});

            list = JSON.parseObject(JSONObject.toJSONString(xiaoNvRtn.getDatas()), new TypeReference<List<XiaoNvOrderDetail>>() {});
            for (XiaoNvOrderDetail xiaoNvOrderDetail: list
            ) {
                xiaoNvOrderDetail.setCaiptp("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605028825784&di=de68af5e1a84e6896b6f2c2801fc2fdd&imgtype=0&src=http%3A%2F%2Fcp2.douguo.net%2Fupload%2Fcaiku%2Fb%2Fd%2Fd%2F600x400_bdf6889987b629b6c85b97a3158610dd.jpg");

                String caipbh = xiaoNvOrderDetail.getMeNO();
                XiaoNCaiPYycf xiaoNCaiPYycf = cpyy(caipbh);
                xiaoNvOrderDetail.setRel(xiaoNCaiPYycf.getReliang());
                xiaoNvOrderDetail.setDanbz(xiaoNCaiPYycf.getDanbaizhi());
                xiaoNvOrderDetail.setZhif(xiaoNCaiPYycf.getZhifang());
                xiaoNvOrderDetail.setTanshhw(xiaoNCaiPYycf.getTanshui());
                xiaoNvOrderDetail.setShansqw(xiaoNCaiPYycf.getQianwei());
                xiaoNvOrderDetail.setDangc(xiaoNCaiPYycf.getDg());
                xiaoNvOrderDetail.setNa(xiaoNCaiPYycf.getNa());
                xiaoNvOrderDetail.setGai(xiaoNCaiPYycf.getGa());

                xiaoNvOrderDetail.setXfMeNa(xiaoNvOrderDetail.getMeName());
                xiaoNvOrderDetail.setXfPirce(xiaoNvOrderDetail.getMePirce());
            }

        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return list;
    }

    // 营养成分
    XiaoNCaiPYycf cpyy(String cpbh) {
        XiaoNCaiPYycf xiaoNCaiPYycf = null;

        Map<String, Object> map = new HashMap() {{
            put("cp_workno", cpbh);
        }};

        String encrypt = jiam(map);
        try {
            String rtn = restTemplate.postForObject(serverUrl + "/cpyy", encrypt, String.class);
            if(rtn == null) {
                return null;
            }

            String decrypt = jiem(rtn);
            xiaoNCaiPYycf = JSON.parseObject(decrypt, new TypeReference<XiaoNCaiPYycf>(){});

        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return xiaoNCaiPYycf;
    }

    // 获取每天的最新菜单
    public List<HqStCaip> cpdateshow() {
        List<HqStCaip> result = new ArrayList<>();
//        wxLdSsCzXf("47055");
//        cpdatamx("95053250-0949-4DFE-BA0C-AC5C010A1B2F");

        // "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
        String[] weekDays = {"1", "2", "3", "4", "5", "6", "7"};
        Calendar calendar = Calendar.getInstance();
        String rq = weekDays[calendar.get(Calendar.DAY_OF_WEEK) - 1];

        Map<String, Object> map = new HashMap() {{
            put("cp_bh", "1");
            put("rq", 1);
        }};

        String encrypt = jiam(map);
        try{
            String rtn = restTemplate.postForObject(serverUrl + "/cpdateshow", encrypt, String.class);
            String decrypt = jiem(rtn);
            XiaoNvRtn xiaoNvRtn = JSON.parseObject(decrypt, new TypeReference<XiaoNvRtn>(){});

            List<XiaoNCaiP> list = new ArrayList<>();
            for (Object obj: xiaoNvRtn.getDatas()
                 ) {
                XiaoNCaiP xiaoNCaiP = JSON.parseObject(obj.toString(), new TypeReference<XiaoNCaiP>(){});
                if(xiaoNCaiP != null)
                    list.add(xiaoNCaiP);
            }

            // 通过菜品编号，拿菜品详情和营养成分
            for (XiaoNCaiP xiaoNCaiP: list
                 ) {
                XiaoNCaiPYycf xiaoNCaiPYycf = cpyy(xiaoNCaiP.getCpnabh());
//                cpdataimg(xiaoNCaiP.getCpnabh());

                HqStCaip hqStCaip = new HqStCaip();
                hqStCaip.setId(xiaoNCaiP.getCpnabh());
                hqStCaip.setName(xiaoNCaiP.getCpname());


//                hqStCaip.setPicture()
                hqStCaip.setPrice(Double.parseDouble(xiaoNCaiP.getCpjg()));
                hqStCaip.setReliang(xiaoNCaiPYycf.getReliang());
                hqStCaip.setDanbaizhi(xiaoNCaiPYycf.getDanbaizhi());
                hqStCaip.setZhifang(xiaoNCaiPYycf.getZhifang());
                hqStCaip.setTanshui(xiaoNCaiPYycf.getTanshui());
                hqStCaip.setShansqw(xiaoNCaiPYycf.getQianwei());
                hqStCaip.setDangc(xiaoNCaiPYycf.getDg());
                hqStCaip.setNa(xiaoNCaiPYycf.getNa());
                hqStCaip.setGai(xiaoNCaiPYycf.getGa());

                hqStCaip.setUpordown("1");

                result.add(hqStCaip);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }


}
